<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Laravel\Sanctum\HasApiTokens;

/**
 * App\Models\AdminUserModel
 *
 * @property int $id
 * @property int $agent_id
 * @property int $level
 * @property string $name
 * @property string $username
 * @property string $password
 * @property string $remember_token
 * @property int $created_at
 * @property int $updated_at
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @property-read int|null $notifications_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\AdminRoleModel[] $roles
 * @property-read int|null $roles_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\Laravel\Sanctum\PersonalAccessToken[] $tokens
 * @property-read int|null $tokens_count
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUserModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUserModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUserModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUserModel whereAgentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUserModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUserModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUserModel whereLevel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUserModel whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUserModel wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUserModel whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUserModel whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AdminUserModel whereUsername($value)
 * @mixin \Eloquent
 */
class AdminUserModel extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $table = 'admin_user';

    protected $dateFormat = 'U';
    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name',
        'user_name',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'created_at' => 'datetime:Y-m-d H:i:s',
    ];

    public function roles(): BelongsToMany
    {
        return $this->belongsToMany(AdminRoleModel::class, 'admin_user_role', 'user_id', 'role_id');
    }

    public function checkPassword(string $value): bool
    {
        return Hash::check($value, $this->password);
    }
}
